using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;

namespace OpenEdge.DatabaseEngineering.Import
{
	/// <summary>
	/// Summary description for OptionsForm.
	/// </summary>
	public class OptionsForm : System.Windows.Forms.Form
	{

		public delegate void ImportRequest(String fileString, String packageString, bool createRels);
		public delegate void LogWriterRequest(String fileString);
		public delegate void CloseLogWriterRequest();
	
		public event ImportRequest			Import;
		public event LogWriterRequest		OpenLogWriter;
		public event CloseLogWriterRequest	CloseLogWriter;

		int totalProgress = 0;

		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		internal System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		internal System.Windows.Forms.Button btnDone;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.CheckBox checkBox2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public OptionsForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

			this.Load +=new EventHandler(OptionsForm_Load);
			this.Closing +=new CancelEventHandler(OptionsForm_Closing);

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.btnDone = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.button3 = new System.Windows.Forms.Button();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox1.Location = new System.Drawing.Point(176, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(352, 23);
			this.textBox1.TabIndex = 13;
			this.textBox1.Text = "";
			
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(144, 24);
			this.label1.TabIndex = 8;
			this.label1.Text = "Schema Definition File";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(528, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(32, 23);
			this.button1.TabIndex = 9;
			this.button1.Text = "...";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(400, 168);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 24);
			this.button2.TabIndex = 10;
			this.button2.Text = "Import";
			this.button2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// btnDone
			// 
			this.btnDone.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnDone.Location = new System.Drawing.Point(488, 168);
			this.btnDone.Name = "btnDone";
			this.btnDone.Size = new System.Drawing.Size(75, 24);
			this.btnDone.TabIndex = 12;
			this.btnDone.Text = "Close";
			this.btnDone.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
			// 
			// textBox3
			// 
			this.textBox3.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox3.Location = new System.Drawing.Point(176, 96);
			this.textBox3.Name = "textBox3";
			this.textBox3.ReadOnly = true;
			this.textBox3.Size = new System.Drawing.Size(352, 23);
			this.textBox3.TabIndex = 14;
			this.textBox3.Text = "";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(112, 96);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 24);
			this.label2.TabIndex = 15;
			this.label2.Text = "Log File";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button3.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button3.Location = new System.Drawing.Point(528, 96);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(32, 23);
			this.button3.TabIndex = 16;
			this.button3.Text = "...";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(16, 240);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(544, 23);
			this.progressBar1.TabIndex = 17;
			// 
			// textBox6
			// 
			this.textBox6.BackColor = System.Drawing.SystemColors.Control;
			this.textBox6.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBox6.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox6.Location = new System.Drawing.Point(24, 208);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(536, 16);
			this.textBox6.TabIndex = 20;
			this.textBox6.Text = "";
			// 
			// checkBox1
			// 
			this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.checkBox1.Location = new System.Drawing.Point(40, 56);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(16, 24);
			this.checkBox1.TabIndex = 21;
			this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
			// 
			// textBox2
			// 
			this.textBox2.Enabled = false;
			this.textBox2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox2.Location = new System.Drawing.Point(176, 56);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(352, 23);
			this.textBox2.TabIndex = 23;
			this.textBox2.Text = "";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(64, 56);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(104, 24);
			this.label4.TabIndex = 24;
			this.label4.Text = "Package Name";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(24, 136);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(144, 24);
			this.label3.TabIndex = 26;
			this.label3.Text = "Create Relationships?";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		
			// 
			// checkBox2
			// 
			this.checkBox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.checkBox2.Location = new System.Drawing.Point(176, 136);
			this.checkBox2.Name = "checkBox2";
			this.checkBox2.Size = new System.Drawing.Size(16, 24);
			this.checkBox2.TabIndex = 25;
			// 
			// OptionsForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(602, 272);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.checkBox2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox6);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnDone);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "OptionsForm";
			this.Text = "Import Schema Definition";
			this.ResumeLayout(false);

		}
		#endregion


		private void button1_Click(object sender, System.EventArgs e)
		{

		    openFileDialog1.InitialDirectory = "c:\\OpenEdgeMDGWork" ;
			openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex = 2 ;
			openFileDialog1.RestoreDirectory = true ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.textBox1.Text = openFileDialog1.FileName;
			}


		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.OpenLogWriter(this.textBox3.Text);

			String packageName = "";
			if (this.checkBox1.Checked && this.textBox2.Text.Length > 0)
				packageName = this.textBox2.Text;

			this.Import(this.textBox1.Text, packageName, this.checkBox2.Checked);
		}

		private void btnDone_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		public void logWriteLineHandler(String text) 
		{
			/*
			if (this.textBox2.TextLength > 30000) 
			{
				this.textBox2.ResetText();
			}
			this.textBox2.AppendText(text + "\n");
			*/

			this.textBox6.Text = text;
			this.textBox6.Refresh();
			
		}

		public void progressLengthHandler(int total)
		{
			this.progressBar1.Minimum = 1;
			this.progressBar1.Maximum = total;
			this.progressBar1.Value   = 1;
		}

		public void progressRequestHandler(int length)
		{			
			this.totalProgress += length;
			this.progressBar1.Value += length;
		}

		public void progressEndHandler()
		{
			this.progressBar1.Value = this.progressBar1.Maximum;
			MessageBox.Show("Schema Definition Import Complete", "Schema Definition Import Complete");
			this.progressBar1.Value = 1;
			this.textBox6.Clear();
			this.CloseLogWriter();
		}


		private void button3_Click(object sender, System.EventArgs e)
		{
			this.saveFileDialog1.InitialDirectory	= "c:\\OpenEdgeMDGWork";
			this.saveFileDialog1.Filter				= "txt files (*.log)|*.log|All files (*.*)|*.*" ;
			this.saveFileDialog1.FilterIndex		= 2;
			this.saveFileDialog1.RestoreDirectory	= true;
			this.saveFileDialog1.OverwritePrompt	= true;


			if(this.saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.textBox3.Text = this.saveFileDialog1.FileName;
			}
		
		}

		private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
		{
			this.textBox2.Enabled = this.checkBox1.Checked;
		}

		private void OptionsForm_Load(object sender, EventArgs e)
		{

			RegistryKey registryKey = Registry.CurrentUser;
			this.textBox3.Text = (string) registryKey.OpenSubKey("Software").OpenSubKey("Sparx Systems").OpenSubKey("EAAddins").OpenSubKey("OpenEdge").GetValue("ImportSchema.LogFile");

		}

		private void OptionsForm_Closing(object sender, CancelEventArgs e)
		{

			RegistryKey registryKey = Registry.CurrentUser;
			registryKey.OpenSubKey("Software").OpenSubKey("Sparx Systems").OpenSubKey("EAAddins").OpenSubKey("OpenEdge", true).SetValue("ImportSchema.LogFile", this.textBox3.Text);

		}


	}
}
